//
//  XJPageTitleView.swift
//  XJZB
//
//  Created by 李胜兵 on 16/9/24.
//  Copyright © 2016年 付公司. All rights reserved.
//  
/** 学习添加手势action的参数和方法的调用不同？
    
   2:注意代理方法名字和类名要有大小写区别，如果方法名和类名一模一样的话，会报错没有声明该类
 */




import UIKit

// MARK: - 定义代理格式继承class只能被类遵守,结构体，枚举之类不可以遵守
protocol XJPageTitleViewDelegate : class {
    func xjPageTitleView(_ titleView : XJPageTitleView, selectedIndex index : Int)
}

private let kScrollLineH : CGFloat = 2
private let kBottonViewLineH : CGFloat = 0.5
private let kNormalColor : (CGFloat , CGFloat , CGFloat) = (85,85,85)  // 灰色
private let kSelectColor : (CGFloat , CGFloat , CGFloat) = (255,128,0) // 橘色




class XJPageTitleView: UIView {

    // MARK: - 定义属性保存
    fileprivate var titles : [String]
    fileprivate var currentIndex : Int = 0
    weak var delegate : XJPageTitleViewDelegate?
    
    // MARK: - 定义并懒加载一个存放label的数组
    fileprivate lazy var titleLabels : [UILabel] = [UILabel]()
    
    
    // MARK: - 懒加载滚动视图
    fileprivate lazy var scrollView : UIScrollView = {
       let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        return scrollView
        
    }()
    
    // MARK: - 懒加载底部滚动线条
    fileprivate lazy var scrollLine : UIView = {
        let scrollLine  = UIView ()
        scrollLine.backgroundColor = UIColor.orange
        return scrollLine
    }()
    
    
    // MARK: - 自定义构造函数
    init(frame: CGRect, titles: [String]) {
        self.titles = titles;
        super.init(frame: frame)
        // 设置UI界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}




// MARK: - 设置UI界面
extension XJPageTitleView {
    fileprivate func setupUI() {
       // 1.添加 UIScrollView
        addSubview(scrollView)
        scrollView.frame = bounds
        
       // 2.添加title对应的label
         setupUITitleLabels()
        
       // 3.设置标题底部线条
        setupTitleBottonLineView()
    }
    
    
    
    fileprivate func setupUITitleLabels() {
        // 0.确定label的一些frame值
        let labelW : CGFloat = frame.width / CGFloat(titles.count)
        let labelH : CGFloat = frame.height - kScrollLineH
        let labelY : CGFloat = 0
        
        for (index , title) in titles.enumerated() {
            // 1创建UILabel
            let label = UILabel()
            
            // 2设置label的属性
            label.text = title
            label.tag = index
            label.font = UIFont.systemFont(ofSize: 16.0)
            label.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
            label.textAlignment = .center
            
            // 3设置label的frame
            let labelX : CGFloat = labelW * CGFloat(index)
            label.frame = CGRect(x: labelX, y: labelY, width: labelW, height: labelH)
            
            // 4将label加到scrollView中
            scrollView.addSubview(label)
            
            // 5.将创建的label加到数组中
            titleLabels.append(label)
            
            // 6.给label添加手势
            label.isUserInteractionEnabled = true
            let tapGes = UITapGestureRecognizer(target: self, action:#selector(XJPageTitleView.titleLabelClick(_:)))
            label.addGestureRecognizer(tapGes)
            
        }
    }
    
    fileprivate func setupTitleBottonLineView() {
        
        // 1.添加底线到view上
        let titleBottonLineView = UIView()
        titleBottonLineView.backgroundColor = UIColor.lightGray
        titleBottonLineView.frame = CGRect(x: 0, y: frame.height - kBottonViewLineH, width: frame.width, height: kBottonViewLineH)
        addSubview(titleBottonLineView)
        
        
        // 2.添加scrollLine到UIScrollView上
        // 2.1获取第一个label
        guard let firstLabel =  titleLabels.first else { return }
        firstLabel.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
        
        scrollView.addSubview(scrollLine)
        scrollLine.frame = CGRect(x: firstLabel.frame.origin.x, y: frame.height - kScrollLineH - kBottonViewLineH, width: firstLabel.frame.width, height: kScrollLineH)
    }
    
}

// MARK: - 监听label的点击事件
extension XJPageTitleView {
    @objc fileprivate func titleLabelClick(_ tapGes : UIGestureRecognizer) {
        
        // 1.获取当前label
        guard let currentLabel = tapGes.view as? UILabel else { return }
        
        // 2.获取上一个的label<可以保存起来或者保存之前的下标值通过那个数组也是可以拿到>
        let oldLabel = titleLabels[currentIndex]
        
        if currentLabel == oldLabel { return }
        // 3.切换文字的颜色
        currentLabel.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
        oldLabel.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
    
        // 4.保存最新的label下标值
        currentIndex = currentLabel.tag
        
        // 5.滚动条位置发生改变
        let scrollLineX = CGFloat(currentLabel.tag) * scrollLine.frame.width
        UIView.animate(withDuration: 0.25, animations: { () -> Void in
            self.scrollLine.frame.origin.x = scrollLineX
        }) 
        
        // 6.通知代理做事情
        delegate?.xjPageTitleView(self, selectedIndex: currentLabel.tag)
    }
}

// MARK: - 外界暴露的方法
extension XJPageTitleView {
    func setTitleViewWithProgress(_ progress : CGFloat , sourceIndex : Int , targetIndex : Int) {
    
        // 1.取出sourceLabel / targetLabel
        let sourceLabel = titleLabels[sourceIndex]
        let targetLabel = titleLabels[targetIndex]
        
        // 2.处理滑块的逻辑
        let moveTotalX = targetLabel.frame.origin.x - sourceLabel.frame.origin.x
        let moveX = moveTotalX * progress
        scrollLine.frame.origin.x = sourceLabel.frame.origin.x + moveX
        
        // 3.颜色的渐变<复杂>
        // 3.1取出颜色变化的范围
        let colorDelta = (kSelectColor.0 - kNormalColor.0, kSelectColor.1 - kNormalColor.1,kSelectColor.2 - kNormalColor.2)
        
        // 3.2变化sourceLabel
        sourceLabel.textColor = UIColor(r: kSelectColor.0 - colorDelta.0 * progress, g: kSelectColor.1 - colorDelta.1 * progress, b: kSelectColor.2 - colorDelta.2 * progress)
        
        // 3.3变化targetLabel
        targetLabel.textColor = UIColor(r: kNormalColor.0 + colorDelta.0 * progress, g: kNormalColor.1 + colorDelta.1 * progress, b: kNormalColor.2 + colorDelta.2 * progress)
    
        // 4.纪录最新的下标值
        currentIndex = targetIndex
    }
}





